Welcome Guest | Sign in | Register

Home > Java Programming > Flow Control > Questions and Answers

01. What will be the output of the program?

int I = 0;
outer:
while (true)
{
I++;
inner:
for (int j = 0; j < 10; j++)
{
I += j;
if (j == 3)
continue inner;
break outer;
}
continue outer;
}
System.out.println(I);
A. 1 B. 2
C. 3 D. 4

Answer and Explanation

Answer: 1

Explanation:
The program flows as follows: I will be incremented after the while loop is entered, then I will be incremented (by zero) when the for loop is entered. The if statement evaluates to false, and the continue statement is never reached. The break statement tells the JVM to break out of the outer loop, at which point I is printed and the fragment is done.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. What will be the output of the program?

int i = O;
while(1)
{
if(i == 4)
{
break;
}
++i;
}
System.out.println("i = " + i);
A. i = 0 B. i = 3
C. i = 4 D. Compilation fails.

Answer and Explanation

Answer: Compilation fails.

Explanation:
Compilation fails because the argument of the while loop, the condition, must be of primitive type boolean. In Java, 1 does not represent the true state of a boolean, rather it is seen as an integer.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. What will be the output of the program?

for(int i = 0; i < 3; i++)
{
switch(i)
{
case 0: break;
case 1: System.out.print("one ");
case 2: System.out.print("two ");
case 3: System.out.print("three ");
}
}
System.out.println("done");
A. done B. one two done
C. one two three done D. one two three two three done

Answer and Explanation

Answer: one two three two three done

Explanation:
The variable i will have the values 0, 1 and 2.
When i is 0, nothing will be printed because of the break in case 0.
When i is 1, "one two three" will be output because case 1, case 2 and case 3will be executed (they don't have break statements).
When i is 2, "two three" will be output because case 2 and case 3 will be executed (again no break statements).
Finally, when the for loop finishes "done" will be output.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. public class While
{
public void loop()
{
int x= 0;
while ( 1 ) /* Line 6 */
{
System.out.print("x plus one is " + (x + 1)); /* Line 8 */
}
}
}

Which statement is true? 
A. There is a syntax error on line 1. B. There are syntax errors on lines 1 and 6.
C. There are syntax errors on lines 1, 6, and 8. D. There is a syntax error on line 6.

Answer and Explanation

Answer: There is a syntax error on line 6.

Explanation:
Using the integer 1 in the while statement, or any other looping or conditional construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java.
A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name). Line 8 is also valid because an equation may be placed in a String operation as shown.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. class A{
Public static void main(String args[]){
System.out.println(“I am in main method”);
}
Public static void test(){
System.out.println(“I am in test method”);
}
}

Which is true?
A. i am in main method B. i am in test method
C. i am in main method
i am in test method
D. i am in main method
i am in test method

Answer and Explanation

Answer: i am in main method

Explanation:
here the code which we have in main method will execute, test method is static memory will be allocated automatically ,as test method we are not calling from main method so it will not execute.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. class A{
Public static void test(){
System.out.println(“I am in test method”);
}

Public static void main(String args[]){
System.out.println(“I am in main method”);
}


Public static void test1(){
System.out.println(“I am in test method”);
}
}

Which will print the output?
A. i am in main method B. i am in test method
C. i am in main method
i am in test method
D. i am in main method
i am in test method

Answer and Explanation

Answer: i am in main method

Explanation:
here the code which we have in main method will execute, JVM will start execution from main method. and test() method or test1() method we are not calling from main method so it will not execute.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. class F{
public static void main(String args[]){
Int i=10;
System.out.println(“value of I is”+i);
}
}
A. value of I is 10 B. compile time error
C. run time error D. no out put

Answer and Explanation

Answer: value of I is 10

Explanation:
Normal execution, JVM will execute from main method and display the value of the local variable i.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. class F
{
Static public void main(String args[])
{
System.out.println(“HI”);
}
}
A. compile time error B. run time error
C. HI D. no output

Answer and Explanation

Answer: HI

Explanation:
Here we have swaped static keyword and public access specifier, that doesn’t effect the execution but if you swap void and main it will throw an error. Normal execution , JVM will execute from main method and displays HI.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. class F
{
public void main(String args[])
{
System.out.println(“HI”);
}
}


A. compile time error
B. run time error
C. HI
D. no output

Answer and Explanation

Answer: run time error

Explanation:
here static keyword is removed from main() method signature, compiler will check whether main() method is declared properly or not , According to the compiler its correct, , because static keyword is not a mandatory for all the methods, its upto the developer to decide when to use static and all . BUT during run time JVM will check the signature of main() method , but its wrong, because JVM will be searching for the method which has public static void main(), so it will throw run time error. 

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. class F{
static void main(String args[]){
System.out.println(“HI”);
}
}
A. compile time error B. run time error
C. HI D. no output

Answer and Explanation

Answer: run time error

Explanation:
here public keyword is removed from main() method signature, compiler will check whether main() method is declared properly or not , According to the compiler its correct, because public access specifier is not a mandatory for all the methods, its upto the developer to decide when to use public and all . BUT during runtime JVM will check the signature of main() method , but its wrong, because JVM will be searching for the method

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved 2012-2015 SoftLucent.